﻿using System;
using System.Linq.Expressions;

namespace EmbeddedLanguage
{

    public interface IHaveMethodBindExpression {
        Expression GetMethodBind();
    }

    public interface IHaveOnNew<T> : IHaveMethodBindExpression
    {
        /// <summary>
        /// Allows consumers of the class to declaratively specify actions to execute during initialization.
        /// </summary>
        ActionList<T> OnNew { set; }

        Expression<Action<T>> MethodBind { get; }

    }

    //public interface IMethodBind<T>
    //{
        
    //}

    public static class OnNewHelper
    {
        public static void OnNew<T>(this T Obj, ActionList<T> actions) where T : IHaveOnNew<T>
        {
            //var typ = typeof(T);
            //var attribs = typ.GetAttributes<MethodBindingAttribute>();
            //foreach (var attrib in attribs)
            //{
            //    var objBinder = Activator.CreateInstance(attrib.BindType) as IBindToObject;
            //    objBinder.Bind(Obj);
            //}
            if (Obj.MethodBind != null)
            {
                var compiledMethodBind = CompiledExpressionCache_0x0<T>.GetCompiledAction(Obj.MethodBind);
                compiledMethodBind(Obj);
            }
            actions.ForEach(action => action(Obj));
        }
    }
}
